package exp3;

import java.util.*;

public class TEXT6 {
    public static Queue<Integer> queueDelete (Queue<Integer> q1, int index){
        Queue<Integer> q2 = new LinkedList<>();
        for (int i = 0; !q1.isEmpty(); i++) {
            if (i == index) {
            	q1.poll();
            } else {
                q2.offer(q1.peek());
                q1.poll();
            }
        }
        return q2;
    }
    public static Queue<Integer> queueInsert (Queue<Integer> q1, int index, int value) {
        Queue<Integer> q2 = new LinkedList<>();
        for (int i = 0; !q1.isEmpty(); i++) {
            if (i == index) {
                q2.offer(value);
            }
                q2.offer(q1.peek());
                q1.poll();
        }
        return q2;
    }
    public static Stack<Integer> stackDelete (Stack<Integer> s1, int index) {
        Stack<Integer> s2 = new Stack<>();
        for (int i = 0; i < index; i++) {
            s2.push(s1.peek());
            s1.pop();
        }
        s1.pop();
        while (!s2.isEmpty()) {
            s1.push(s2.peek());
            s2.pop();
        }
        return s1;
    }

    public static Stack<Integer> stackInsert (Stack<Integer> s1, int index, int value) {
        Stack<Integer> s2 = new Stack<>();
        for (int i = 0; i < index; i++) {
            s2.push(s1.peek());
            s1.pop();
        }
        s1.push(value);
        for (int i = 0; !s2.isEmpty(); i++) {
            s1.push(s2.peek());
            s2.pop();
        }
        return s1;
    }

    public static void main(String[] args) {
        // Stack
        Stack<Integer> s = new Stack<>();
        s.push(1);
        s.push(2);
        s.push(3);
        s.push(4);
        System.out.println("Stack: ");
        System.out.println(s);
        s = stackDelete(s, 1);
        System.out.println(s);
        s = stackDelete(s, 1);
        System.out.println(s);
        s = stackInsert(s, 1, 5);
        System.out.println(s);

        // Queue
        Queue<Integer> q = new LinkedList<>();
        q.offer(1);
        q.offer(2);
        q.offer(3);
        q.offer(4);
        System.out.println("Queue: ");
        System.out.println(q);
        q = queueDelete(q, 1);
        System.out.println(q);
        q = queueDelete(q, 1);
        System.out.println(q);
        q = queueInsert(q, 1, 5);
        System.out.println(q);
    }
}
